<%@ page import="com.party.core.model.system.TargetType" %>
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="../include/tag.jsp"%>
<!DOCTYPE html>
<html lang="zh">
<head>
    <title>${category == null ? '发布' : '编辑'}导购类型</title>
    <%@include file="../include/commonFile.jsp"%>
    <link rel="stylesheet" href="${ctx}/css/common/list.css">
    <link rel="stylesheet" href="${ctx}/css/ui/activity/publish_form.css">
</head><body>
<!--头部-->
<%@include file="../include/header.jsp"%>
<div class="index-outside">
    <%@include file="../include/sidebar.jsp"%>
    <!--内容-->
    <section>
        <div class="section-main">
            <!-- 正文请写在这里 -->
            <div class="add-form-content">
                <form id="myForm" class="layui-form mt20" method="post" action="${ctx}/mall/showcategory/save.do">
                    <input type="hidden" name="id" value="${category.id}" />
                    <input type="hidden" name="subjectId" id="subjectId" value="${subjectId}" />
                    <input type="hidden" name="templateId" id="templateId" value="${templateId}"/>
                    <div class="layui-form-item">
                        <label class="layui-form-label">类型名称<%--<span class="f-verify-red">*</span>--%></label>
                        <div class="layui-input-block">
                            <input type="text" name="name" lay-verify="name" autocomplete="off"
                                   placeholder="类型名称" class="layui-input" value="${category.name}"
                            >
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">图片<span class="f-verify-red">*</span></label>
                        <div class="cover-content">
                            <input type="hidden" name="pic" id="pic" lay-verify="pic" value="${category.pic}" />
                            <c:if test="${category == null || empty category.pic}">
                                <span id="cover-img" class="cover-img" style="background-image:url(${ctx}/image/posterImg.png)"></span>
                            </c:if>
                            <c:if test="${category != null && not empty category.pic}">
                                <span id="cover-img" class="cover-img" style="background-image:url('${category.pic}')"></span>
                            </c:if>
                            <a class="layui-btn layui-btn-danger" id="sel_pic_img">+添加图片</a>
                            <div class="form-word-aux">建议尺寸：800x450</div>
                        </div>
                    </div>
                    

                    <div class="layui-form-item">
                        <label class="layui-form-label">排序<span class="f-verify-red">*</span></label>
                        <div class="layui-input-inline">
                            <input type="text" name="sort" lay-verify="sort" autocomplete="off"
                                   placeholder="排序" class="layui-input" value="${category.sort}"
                            >
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <a href="javascript:void(0)" class="layui-btn layui-btn-danger" lay-submit lay-filter="*">立即提交</a>
                            <c:if test="${!empty subjectId}">
                                <a href="${ctx}/mall/showcategory/list.do?subjectId=${subjectId}" class="layui-btn layui-btn-primary">取消</a>
                            </c:if>
                            <c:if test="${!empty templateId}">
                                <a href="${ctx}/mall/showtemplatecategory/list.do?templateId=${templateId}" class="layui-btn layui-btn-primary">取消</a>
                            </c:if>
                        </div>
                    </div>
                </form>
            </div>
        </div><!--底部-->
        <%@include file="../include/footer.jsp"%>
    </section>
</div>
<script type="text/javascript" src="${ctx}/script/common/form_cache.js"></script>
<script>
    var form;
    $(function() {
        layui.use([ 'form', 'laydate' ], function() {
            form = layui.form, laydate = layui.laydate;

            //自定义验证规则
            form.verify({
                /*name : function(value) {
                    if (value == "" || !value.trim()) {
                        return '商品名不能为空';
                    }
                },*/
                pic : function(value) {
                    if (value == "" || !value.trim()) {
                        return "图片不能为空";
                    }
                },
                sort : function(value) {
                    if (value == "" || !value.trim()) {
                        return "排序不能为空";
                    }
                },
                /* categoryId : function(value) {
                    if (value == "") {
                        return "请选择类型";
                    }
                }, */
            });

            //监听提交
            form.on('submit', function (data) {
            	 /* if(checkCost()) {
                } */
                /* resetIndex(); */
                submitForm(data);
            });

            function submitForm(data) {
                txz.submitObject(data.elem, function (callBack) {
                    var formData = getFormData();
                    var action = $("#myForm").attr("action");
                    txz.ajaxRequest({
                        method: 'post',
                        url: action,
                        saveCache: false,
                        params: formData,
                        callBack: function (res) {
                            typeof callBack === 'function' && callBack();
                            if (res.success) {
                                util.layerMsgSuccess("提交成功", function () {
                                    if ($('#subjectId').val()) {
                                        location.href = "${ctx}/mall/showcategory/list.do?subjectId=${subjectId}";
                                    }
                                    if ($('#templateId').val()) {
                                        location.href = "${ctx}/mall/showtemplatecategory/list.do?templateId=${templateId}";
                                    }
                                });
                            } else {
                                util.layerMsgError("提交失败")
                            }
                        }
                    })
                });
            }
        });

        $('#sel_pic_img').click(function () {
            txz.openSelImg({
                min: 1,
                max: 1,
                info: '建议尺寸：800x450',
                type: '<%=TargetType.MALL.getCode()%>',
                cb: function (imgs) {
                    $('#cover-img').css('background-image', 'url(' + imgs[0].path + ')');
                    $('#pic').val(imgs[0].path);
                }
            })
        })
    })



    //此方法用于加载缓存数据
    function loadCacheData(cacheData) {
        //加载封面图
        if (cacheData['pic']) {
            $('#cover-img').css('background-image', 'url(' + cacheData['pic'] + ')');
        }
        form.render();
    }

    function getFormData() {
        return util.serializeForm($('#myForm').serializeArray());
    }
</script>
<script type="text/javascript">
    txz.initHeader({
        <c:if test="${!empty subjectId}">
        nav:[{
            name: '导购主题',
            href:'${ctx}/mall/showsubject/list.do'
        },{
            name: '类型管理',
            href: '${ctx}/mall/showcategory/list.do?subjectId=${subjectId}'
        },{
            name: '${category == null ? '发布' : '编辑'}导购类型',
            curr: true
        }],
        btns:[{
            type: 'back'
        }]
        </c:if>
        <c:if test="${!empty templateId}">
        nav:[{
            name: '主题分类',
            href:'${ctx}/mall/showtemplate/list.do'
        },{
            name: '类型管理',
            href: '${ctx}/mall/showtemplatecategory/list.do?templateId=${templateId}'
        },{
            name: '${category == null ? '发布' : '编辑'}主题类型',
            curr: true
        }],
        btns:[{
            type: 'back'
        }]
        </c:if>
    });
</script>
</body>
</html>